package com.wuyou.simple;

/**
 * 367. 有效的完全平方数
 */
public class IsPerfectSquare {

    public static void main(String[] args) {

        int num = 16;
        System.out.println(soltion_3(num));
    }


    // 2023-3-3 二分?
    public static boolean soltion_3(int num) {
        long l = 1, r = Integer.MAX_VALUE, mid = Integer.MAX_VALUE / 2;
        while (l <= r) {
            long multi = mid * mid;
            if (multi == num) {
                return true;
            }
            if (l * l <= num && num < multi) {
                r = mid - 1;
                mid = ((r - l) / 2) + l;
            }else {
                l = mid + 1;
                mid = ((r - l) / 2) + l;
            }
        }
        return false;
    }

    // 2023-3-3
    public boolean soltion_2(int num) {
        long x = 1;

        while (true) {
            long m = x * x;
            if (m == num) {
                return true;
            }
            if (m > num) {
                return false;
            }
            x++;
        }
    }

    // 六个月前
    public boolean soltion_1(int num) {
        double sqrt = Math.sqrt(num);
        int temp = (int) sqrt;
        String str = sqrt + "";
        String[] split = str.split(temp + ".");
        return "0".equals(split[1]);
    }
}
